//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "VolatilityBumpInstrumentJacobian.h"
using namespace Cephei::QL::Models::Marketmodels::Pathwisegreeks;
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/Models/Marketmodels/Pathwisegreeks/VegaBumpCollection.h>
#include <gen/QL/Models/Marketmodels/Pathwisegreeks/VolatilityBumpInstrumentJacobianSwaption.h>
#include <gen/QL/Models/Marketmodels/Pathwisegreeks/VolatilityBumpInstrumentJacobianCap.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CVolatilityBumpInstrumentJacobian::CVolatilityBumpInstrumentJacobian (Cephei::QL::Models::Marketmodels::Pathwisegreeks::IVegaBumpCollection^ bumps, Cephei::Core::IVector<Cephei::QL::Models::Marketmodels::Pathwisegreeks::IVolatilityBumpInstrumentJacobianSwaption^>^ swaptions, Cephei::Core::IVector<Cephei::QL::Models::Marketmodels::Pathwisegreeks::IVolatilityBumpInstrumentJacobianCap^>^ caps) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CVegaBumpCollection^ _Cbumps;
    try
    {
#ifdef HANDLE
        _phVolatilityBumpInstrumentJacobian = NULL;
#endif
        _Cbumps = safe_cast<CVegaBumpCollection^> (bumps);
        _Cbumps->Lock();
        QuantLib::VegaBumpCollection& _bumps = static_cast<QuantLib::VegaBumpCollection&> (_Cbumps->GetReference ()); 
        swaptions ->Lock ();
        INativeVector<Cephei::QL::Models::Marketmodels::Pathwisegreeks::IVolatilityBumpInstrumentJacobianSwaption^>^ _NCIswaptions = swaptions->getFeature (NativeFeature::Value);
        CVolatilityBumpInstrumentJacobianSwaptionVector^ _NCswaptions = safe_cast<CVolatilityBumpInstrumentJacobianSwaptionVector^>(_NCIswaptions);
        std::vector<QuantLib::VolatilityBumpInstrumentJacobianSwaption>& _swaptions = static_cast<std::vector<QuantLib::VolatilityBumpInstrumentJacobianSwaption>&> (_NCswaptions->GetReference ());
        caps ->Lock ();
        INativeVector<Cephei::QL::Models::Marketmodels::Pathwisegreeks::IVolatilityBumpInstrumentJacobianCap^>^ _NCIcaps = caps->getFeature (NativeFeature::Value);
        CVolatilityBumpInstrumentJacobianCapVector^ _NCcaps = safe_cast<CVolatilityBumpInstrumentJacobianCapVector^>(_NCIcaps);
        std::vector<QuantLib::VolatilityBumpInstrumentJacobianCap>& _caps = static_cast<std::vector<QuantLib::VolatilityBumpInstrumentJacobianCap>&> (_NCcaps->GetReference ());
        _ppVolatilityBumpInstrumentJacobian = new boost::shared_ptr<QuantLib::VolatilityBumpInstrumentJacobian> (new QuantLib::VolatilityBumpInstrumentJacobian ( _bumps,  _swaptions,  _caps ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbumps != nullptr) _Cbumps->Unlock();
        if (swaptions != nullptr) swaptions->Unlock();    //not optional
        if (caps != nullptr) caps->Unlock();    //not optional
    }
}
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CVolatilityBumpInstrumentJacobian::CVolatilityBumpInstrumentJacobian (boost::shared_ptr<QuantLib::VolatilityBumpInstrumentJacobian>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phVolatilityBumpInstrumentJacobian = NULL;
#endif
	_ppVolatilityBumpInstrumentJacobian = &childNative;
    
}
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CVolatilityBumpInstrumentJacobian::CVolatilityBumpInstrumentJacobian (QuantLib::VolatilityBumpInstrumentJacobian& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phVolatilityBumpInstrumentJacobian = NULL;
#endif
	_ppVolatilityBumpInstrumentJacobian = new boost::shared_ptr<QuantLib::VolatilityBumpInstrumentJacobian> (&childNative);
    
    _VolatilityBumpInstrumentJacobianOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::Pathwisegreeks::CVolatilityBumpInstrumentJacobian::CVolatilityBumpInstrumentJacobian (CVolatilityBumpInstrumentJacobian^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phVolatilityBumpInstrumentJacobian = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppVolatilityBumpInstrumentJacobian = new boost::shared_ptr<QuantLib::VolatilityBumpInstrumentJacobian> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CVolatilityBumpInstrumentJacobian::CVolatilityBumpInstrumentJacobian (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phVolatilityBumpInstrumentJacobian = NULL;
#endif
	if (!t->IsSubclassOf(CVolatilityBumpInstrumentJacobian::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CVolatilityBumpInstrumentJacobian::CVolatilityBumpInstrumentJacobian (QuantLib::Handle<QuantLib::VolatilityBumpInstrumentJacobian>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phVolatilityBumpInstrumentJacobian = &childNative;
	_ppVolatilityBumpInstrumentJacobian = &static_cast<boost::shared_ptr<QuantLib::VolatilityBumpInstrumentJacobian>>(childNative.currentLink());
    
    _VolatilityBumpInstrumentJacobianOwner = owner;
}
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CVolatilityBumpInstrumentJacobian::CVolatilityBumpInstrumentJacobian (QuantLib::Handle<QuantLib::VolatilityBumpInstrumentJacobian> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phVolatilityBumpInstrumentJacobian = &childNative;
	_ppVolatilityBumpInstrumentJacobian = &static_cast<boost::shared_ptr<QuantLib::VolatilityBumpInstrumentJacobian>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CVolatilityBumpInstrumentJacobian::CVolatilityBumpInstrumentJacobian (QuantLib::VolatilityBumpInstrumentJacobian childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phVolatilityBumpInstrumentJacobian = NULL;
#endif
	_ppVolatilityBumpInstrumentJacobian = new boost::shared_ptr<QuantLib::VolatilityBumpInstrumentJacobian> (new QuantLib::VolatilityBumpInstrumentJacobian (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::Pathwisegreeks::CVolatilityBumpInstrumentJacobian::~CVolatilityBumpInstrumentJacobian ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppVolatilityBumpInstrumentJacobian != NULL)
    {
	    delete _ppVolatilityBumpInstrumentJacobian;
        _ppVolatilityBumpInstrumentJacobian = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CVolatilityBumpInstrumentJacobian::!CVolatilityBumpInstrumentJacobian ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppVolatilityBumpInstrumentJacobian != NULL)
    {
	    delete _ppVolatilityBumpInstrumentJacobian;
    }
}
QuantLib::VolatilityBumpInstrumentJacobian& Cephei::QL::Models::Marketmodels::Pathwisegreeks::CVolatilityBumpInstrumentJacobian::GetReference ()
{
    if (_ppVolatilityBumpInstrumentJacobian == NULL) throw REFNEW NativeNullException ();
	return **_ppVolatilityBumpInstrumentJacobian;
}
boost::shared_ptr<QuantLib::VolatilityBumpInstrumentJacobian>& Cephei::QL::Models::Marketmodels::Pathwisegreeks::CVolatilityBumpInstrumentJacobian::GetShared ()
{
    if (_ppVolatilityBumpInstrumentJacobian == NULL) throw REFNEW NativeNullException ();
	return *_ppVolatilityBumpInstrumentJacobian;
}
QuantLib::VolatilityBumpInstrumentJacobian* Cephei::QL::Models::Marketmodels::Pathwisegreeks::CVolatilityBumpInstrumentJacobian::GetPointer ()
{
    if (_ppVolatilityBumpInstrumentJacobian == NULL) throw REFNEW NativeNullException ();
	return &**_ppVolatilityBumpInstrumentJacobian;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::VolatilityBumpInstrumentJacobian>& Cephei::QL::Models::Marketmodels::Pathwisegreeks::CVolatilityBumpInstrumentJacobian::GetHandle ()
{
	if (_phVolatilityBumpInstrumentJacobian == NULL)
	{
		_phVolatilityBumpInstrumentJacobian = new Handle<QuantLib::VolatilityBumpInstrumentJacobian> (*_ppVolatilityBumpInstrumentJacobian);
	}
	return *_phVolatilityBumpInstrumentJacobian;
}
#endif
bool Cephei::QL::Models::Marketmodels::Pathwisegreeks::CVolatilityBumpInstrumentJacobian::HasNative () 
{
	return (_ppVolatilityBumpInstrumentJacobian != NULL);
}

Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::Pathwisegreeks::CVolatilityBumpInstrumentJacobian::DerivativesVolatility (UInt64 j)
{
    try
    {
        QuantLib::Size _j = (QuantLib::Size)ValueHelper::Convert (j); //a
    	std::vector<QuantLib::Real> _rv = (std::vector<QuantLib::Real>)(*_ppVolatilityBumpInstrumentJacobian)->derivativesVolatility ( _j );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IMatrix^ Cephei::QL::Models::Marketmodels::Pathwisegreeks::CVolatilityBumpInstrumentJacobian::GetAllOnePercentBumps::get ()
{
    try
    {
    	QuantLib::Matrix& _rv = (QuantLib::Matrix&)(*_ppVolatilityBumpInstrumentJacobian)->getAllOnePercentBumps ( );   
        Cephei::QL::Math::CMatrix^ _nrv = REFNEW Cephei::QL::Math::CMatrix (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Pathwisegreeks::IVegaBumpCollection^ Cephei::QL::Models::Marketmodels::Pathwisegreeks::CVolatilityBumpInstrumentJacobian::GetInputBumps::get ()
{
    try
    {
    	QuantLib::VegaBumpCollection& _rv = (QuantLib::VegaBumpCollection&)(*_ppVolatilityBumpInstrumentJacobian)->getInputBumps ( );   
        Cephei::QL::Models::Marketmodels::Pathwisegreeks::CVegaBumpCollection^ _nrv = REFNEW Cephei::QL::Models::Marketmodels::Pathwisegreeks::CVegaBumpCollection (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::Pathwisegreeks::CVolatilityBumpInstrumentJacobian::OnePercentBump (UInt64 j)
{
    try
    {
        QuantLib::Size _j = (QuantLib::Size)ValueHelper::Convert (j); //a
    	std::vector<QuantLib::Real> _rv = (std::vector<QuantLib::Real>)(*_ppVolatilityBumpInstrumentJacobian)->onePercentBump ( _j );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Pathwisegreeks::IVolatilityBumpInstrumentJacobian^ Cephei::QL::Models::Marketmodels::Pathwisegreeks::CVolatilityBumpInstrumentJacobian_Factory::Create (Cephei::QL::Models::Marketmodels::Pathwisegreeks::IVegaBumpCollection^ bumps, Cephei::Core::IVector<Cephei::QL::Models::Marketmodels::Pathwisegreeks::IVolatilityBumpInstrumentJacobianSwaption^>^ swaptions, Cephei::Core::IVector<Cephei::QL::Models::Marketmodels::Pathwisegreeks::IVolatilityBumpInstrumentJacobianCap^>^ caps)
{
    return REFNEW CVolatilityBumpInstrumentJacobian ( bumps,  swaptions,  caps);
}
